<?php

/**
 * Klasa mapująca tabelę z bazy danych.
 * 
 * @method array selectOne
 * @method array _assignData
 * @method array selectAll
 * @method array selectOneL
 * @method array selectAllL
 * @method array getLogin
 * @method int zDelete
 * @method int zUpdate
 * @method int zInsert
 * @method string renderujKolumny
 * @method string renderujKolumny2
 * @category Zend_Db_Table_Abstract
 * @package models
 * @version $Rev:$
 */
class admMaTeaser extends Zend_Db_Table_Abstract
{



    const cl_id = 'adm_ma_teaser_id';
    
    const cl_mail = 'adm_ma_teaser_mail';
    
    const cl_kod = 'adm_ma_teaser_kod';

    public $fields_list = array(

        'adm_ma_teaser_id' => array(
            'bigint',
            '0',
            'NOT_NULL'
            ),
        'adm_ma_teaser_mail' => array(
            'CHARACTER VARYING',
            '50',
            'NOT_NULL'
            ),
        'adm_ma_teaser_kod' => array(
            'CHARACTER VARYING',
            '32',
            'NOT_NULL'
            )
        );

    const CLASS_name = 'admMaTeaser';

    public $_name = 'adm_ma_teaser';

    public $PK = 'adm_ma_teaser_id';

    public $fk_list = array(

        );

    public $const_list = array(

        );

    public $nazwy_kolumn = '';

    /**
     * Metoda pobiera rekord o zadanym id lub tablicy z warunkami klauzuli where.
     * 
     * @param int|String $p_id
     * @return array
     */
    public function selectOne($p_id = NULL, $p_where = array())
    {
        $select = $this->select();
        if (isset($p_id))
        {
        	if (strpos($this->PK,'kod')>0)
        		$select->where($this->renderujkolumny($this->PK).' like \''.$p_id.'\'');
        
        	else
        		$select->where($this->renderujkolumny($this->PK).' = '.$p_id);
        }
        
        foreach ($p_where as $variable) {
        	$select->where($this->renderujkolumny($variable));
        }$s = $this->fetchRow( $select );
        if ($s!=NULL)
        	return $s->toArray();
        else
         return $s;
    }

    /**
     * Metoda przypisuje dane przekazane w zmiennej $data do obiektu
     * Zend_Db_Table_Abstract. Metoda zapewnia, że zostaną przypisane tylko dane o
     * tych indeksach które są identyczne z kolumnami w tabeli.
     * 
     * @param array $data
     * @return Zend_DB_Row
     */
    private function _assignData($data, $row)
    {
        foreach( $data as $key => $value )
        {
        		if( array_key_exists( $key, $this->fields_list ) )
        		{
        		 if (get_magic_quotes_gpc()) $value = stripslashes($value);		$row->{$this->renderujkolumny($key)} = $value;
        		}
        }
        return $row;
    }

    /**
     * Metoda do zwracania wszystkich rekordów spełniających kryteria.
     * 
     * @param int $numberOfPages
     * @return Zend_DB_Row
     */
    public function selectAll($numberOfPages = NULL
							, $rowCount = NULL
							, $p_where = array()
							, $p_from = array()
							, $p_order = array()
							, $p_group = array()
							, $p_join = array())
    {
        $licznik = 1;
        $kolumna = 'k';
        
        $select = $this->select()->setIntegrityCheck(false);
        if (isset($numberOfPages)&&isset($rowCount))
        	$select ->limitPage( $numberOfPages, $rowCount );
        
        foreach ($p_from as $value  => $v){
        	$this->nazwy_kolumn[] = (string)$value;
        	$select->from(array($kolumna.$licznik => $value), $this->renderujkolumny($v));
        	$licznik++;
        }
        
        foreach ($p_join as $value  => $v){
        	$this->nazwy_kolumn[] = (string)$value;
        	$select->join(array($kolumna.$licznik => $value), $this->renderujkolumny($v));
        }
        
        foreach ($p_where as $value){
        	$select->where($this->renderujkolumny($value));
        }
        
        foreach ($p_order as $value){
        	$select->order($this->renderujkolumny($value));
        }
        
        foreach ($p_group as $value){
        	$select->group($this->renderujkolumny($value));
        }
        
        if ($numberOfPages==NULL)
        {
        	$s = $this->fetchAll($select);
        	if ($s)
        	{
        		return $s->toArray();
        	}
        	return $s;
        }
        else
        	return $select;
    }

    /**
     * Metoda pobiera rekord o zadanym id i w odpowiedniej wersji językowej.
     * 
     * @return array
     */
    public function selectOneL($p_id
							, $p_class_name_lang
							, $p_lang_kod
							)
    {
        $lang = new $p_class_name_lang;
        
        $ref_list = $this->ref_list;
        $p_lang_fk = $lang->fk_list;
        
        $select = $this->select()->setIntegrityCheck(false)
        			 ->from(array('p' => $this->_name), '*')
                     ->from(array('l' => $lang->_name), '*');
        
        for ($i=0;$i<count($ref_list);$i++)
        {
        	if ($ref_list[$i]==$lang->_name)
        	{
        		$select->where(' l.'.$this->renderujkolumny($ref_list[$i+1]).' like p.'.$this->renderujkolumny($this->PK));
        	}
        }
        
        for ($i=0;$i<count($p_lang_fk);$i++)
        {
        	if (strpos($p_lang_fk[$i],'wersja_jezyk_kod_fk')!== false)
        	{
        		$select->where($this->renderujkolumny($p_lang_fk[$i]).' like \''.$p_lang_kod.'\'');
        	}
        }
        
        $select->where($this->renderujkolumny($this->PK).' like \''.$p_id.'\'');
        
        if ($s)
        {
        	return $s->toArray();
        }
        return $s;
    }

    /**
     * Metoda do zwracania wszystkich rekordów spełniających kryteria w odpowiedniej
     * wersji językowej.
     * 
     * @param string $p_class_name_lang
     * @return array
     */
    public function selectAllL($p_class_name_lang
							, $p_lang_kod
							, $numberOfPages = NULL
							, $rowCount = NULL
							, $p_where = array()
							, $p_from = array()
							, $p_order = array()
							, $p_group = array()
							, $p_join_left = array())
    {
        $licznik = 1;
        $kolumna = 'k';
        
        $lang = new $p_class_name_lang;
        
        $ref_list = $this->ref_list;
        $p_lang_fk = $lang->fk_list;
        
        foreach ($p_from as $value  => $v){
        	$this->nazwy_kolumn[] = (string)$value;
        	$select->from(array($kolumna.$licznik => $value), $this->renderujkolumny($v));
        	$licznik++;
        }
        
        foreach ($p_join as $value  => $v){
        	$this->nazwy_kolumn[] = (string)$value;
        	$select->join(array($kolumna.$licznik => $value), $this->renderujkolumny($v));
        }
        
        $select = $this->select()->setIntegrityCheck(false)
        			 ->from(array('p' => $this->_name), '*')
                    ->from(array('l' => $lang->_name), '*');
        
        for ($i=0;$i<count($ref_list);$i++)
        {
        	if ($ref_list[$i]==$lang->_name)
        	{
        		$select->where(' l.'.$this->renderujkolumny($ref_list[$i+1]).' like p.'.$this->renderujkolumny($this->PK));
        	}
        }
        
        for ($i=0;$i<count($p_lang_fk);$i++)
        {
        	if (strpos($p_lang_fk[$i],'wersja_jezyk_kod_fk')!== false)
        	{
        		$select->where($this->renderujkolumny($p_lang_fk[$i]).' like \''.$p_lang_kod.'\'');
        	}
        }
        
        if (isset($numberOfPages)&&isset($rowCount))
        	$select ->limitPage( $numberOfPages, $rowCount );
        
        foreach ($p_order as $value){
        	$select->order($this->renderujkolumny($value));
        }
        
        foreach ($p_group as $value){
        	$select->group($this->renderujkolumny($value));
        }
        
        if ($numberOfPages==NULL)
        {
        	$s = $this->fetchAll($select);
        	if ($s)
        	{
        		return $s->toArray();
        	}
        	return $s;
        }
        else
        return $select;
    }

    /**
     * Metoda zwracajaca w tablicy id i login zalogowanego użytkownika.
     * 
     * @param unknown
     * @return array
     */
    public function getLogin()
    {
        $auth = Zend_Auth::getInstance();
        $user = $auth->getIdentity();
        
        $usr_id = $user->{admUzUzytkownicy::cl_id};
        $usr_login = $user->{admUzUzytkownicy::cl_login};
        
        $table = Array($usr_id,$usr_login);
        return $table;
    }

    /**
     * Metoda wykonuje operację DELETE ze zdefiniowanym kluczem głównym lub
     * warunkiem where. Zwraca ilosć usuniętych rekordów.
     * 
     * @param int $id
     * @return int
     */
    public function zDelete($id, $p_where)
    {
        if ($id!=NULL)
        {
        	if (strpos($this->PK,'kod')>0)
        	{
        		return $this->delete( $this->renderujkolumny($this->PK).' like \''.$id.'\'' );
        	}
        	else
        	{
        		return $this->delete( $this->renderujkolumny($this->PK).' = '.$id );
        	}
        }
        else
        {
        	return $this->delete( $this->renderujkolumny($p_where));
        }
    }

    /**
     * Metoda wykonuje operacje UPDATE dla jednego lub więcej rekordow.
     * 
     * @param int $id
     * @return int
     */
    public function zUpdate($id, $data, $p_where = array())
    {
        $select = $this->select();
        
        if ($id!=NULL)
        {
        	if (strpos($this->PK,'kod')>0)
        	{
        		$select->where( $this->renderujkolumny($this->PK).' like \''.$id.'\'' );
        	}
        	else
        	{
        		$select->where( $this->renderujkolumny($this->PK).' = '.$id );
        	}
        }
        
        foreach ($p_where as $value){
        	$select->where($this->renderujkolumny($value));
        }
        
        $user_table = $this->getLogin();
        
        if (isset($this->modd))
        {
        	$data[ $this->renderujkolumny($this->modd)] = date('Y-m-d H:i:s');
        	$data[ $this->renderujkolumny($this->modp)] = $user_table[1];
        }
        $s = $this->fetchRow( $select );
        $this->_assignData( $data, $s );
        
        return $s->save();
    }

    /**
     * Metoda wykonuje operację INSERT na bazie danych i zwraca lastInserted id.
     * 
     * @param array $data
     * @return int
     */
    public function zInsert($data)
    {
        $user_table = $this->getLogin();
        
        if (isset($this->insd))
        {
        	$data[ $this->renderujkolumny($this->insd)] = date('Y-m-d H:i:s');
        	$data[ $this->renderujkolumny($this->insp)] = $user_table[1];
        	$data[ $this->renderujkolumny($this->modd)] = date('Y-m-d H:i:s');
        	$data[ $this->renderujkolumny($this->modp)] = $user_table[1];
        }
        $row = $this->createRow();
        
        $this->_assignData( $this->renderujkolumny($data), $row );
        return $row->save();
    }

    /**
     * Metoda wycinająca nazwę tabeli z kolumny (stałych mapera)
     * 
     * @param string $p_ciag
     * @return string
     */
    public function renderujKolumny($p_ciag)
    {
        $ciag_temp = '';
        if ($this->nazwy_kolumn!='')
        {
        	foreach ($this->nazwy_kolumn as $tab_nazwa)
        	{
        		$ciag_temp = str_replace($tab_nazwa.'_','',$p_ciag);
        	}
        }
        
        if ($ciag_temp!='')
        	return str_replace($this->_name.'_','',$ciag_temp);
        else
        	return str_replace($this->_name.'_','',$p_ciag);
    }

    /**
     * Metoda uzupełniająca nazwę kolumny nazwą tabeli
     * 
     * @param string $p_ciag
     * @return string
     */
    public function renderujKolumny2($p_ciag)
    {
        return $this->_name.'_'.$p_ciag;
    }


}

